import pandas as pd
import os
import numpy as np
import shutil

'''
    移除前缘斜率较大的翼型
'''

# 计算翼型上表面斜率
read_dir = r"D:\data_cmp\airfoil_csv_op\airfoil_csv_op_11_bspline"
to_dir = r"D:\data_cmp\airfoil_csv_op\airfoil_csv_op_11_removek_100"

# 移除个数
n = 100


def cal_mean_k(file="goe670.csv"):
    '''
        计算平均斜率
    :param file:
    :return:
    '''

    # read ordinates
    ordinates = pd.read_csv(os.path.join(read_dir, file), index_col=None, header=0).to_numpy()
    # plt.scatter(ordinates[:, 0], ordinates[:, 1])
    # plt.show()

    trailing_low = ordinates[150:160][::-1]  # 下表面

    # cal k
    k_list = []
    for i in range(trailing_low.shape[0] - 1):
        tmp = trailing_low[i + 1] - trailing_low[i]
        k_list.append(np.abs(tmp[1] / tmp[0]))

    return np.mean(k_list)


mean_list = []
file_list = []
for file in os.listdir(read_dir):
    mean = cal_mean_k(file)
    mean_list.append(mean)
    file_list.append(file)
    # print(mean)

# asarray
mean_list = np.asarray(mean_list)
file_list = np.asarray(file_list)

sort_id = np.argsort(mean_list)
mean_list = mean_list[sort_id]
file_list = file_list[sort_id]

print("***********")
print(mean_list[-n::])
print(np.max(mean_list))

# remove to to_dir
for file in file_list[-n::]:
    shutil.move(os.path.join(read_dir, file),
                os.path.join(to_dir, file))
